/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	fetp.c
**  Description:		fpga embed transmit protocol	FPGA与中位机电机协议
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-11-26
**	Function List： 
**  History:         
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/
#include "can_callback.h"
#include "can_protocol.h"
/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	结构体定义
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	函数原型声明
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              	全局变量定义
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	模块静态变量定义
*********************************************************************************************************
*/
static can_pct_callback_t m_can_pct_cb = {0};
/*
*********************************************************************************************************
*                                              	函数定义
*********************************************************************************************************
*/
void can_pct_regist(can_bus_t type,uint16_t node_id,pfn_can_pct_callback tx_callback,pfn_can_pct_callback rx_callback)
{
	ASSERT(type <= CAN_BUS_PERIPH); 
	can_pct_pfn_node_t* p_node = malloc(sizeof(can_pct_pfn_node_t));
	if(p_node == NULL)
	{
		Critical_Log("Malloc failed!\r\n");
		return;
	}
	rt_base_t it = rt_hw_interrupt_disable();

	node_id = CAN_ID_GET_NODE(node_id);
	p_node->can_id = node_id;
	p_node->bus_type = type;
	p_node->tx_callback = tx_callback;
	p_node->rx_callback = rx_callback;
	p_node->next = NULL;
	can_pct_pfn_node_t* p_tail = m_can_pct_cb.p_head;
	if(p_tail == NULL)
	{
		m_can_pct_cb.p_head = p_node;
	}
	else
	{
		while(1)//p_tail->next != NULL)
		{
			if(p_tail->bus_type == type && p_tail->can_id == node_id)
			{
				/*节点已经定义*/
//				Critical_Log("Can callback redefine!\r\n");
				free(p_node);
				rt_hw_interrupt_enable(it);
				return;
			}
			if(p_tail->next != NULL)
			{
				p_tail = p_tail->next;
				continue;
			}
			else
			{
				break;
			}
		}
		p_tail->next = p_node;
	}
	rt_hw_interrupt_enable(it);
}
void can_pct_tx_callback(can_bus_t type,uint16_t can_id,uint8_t dlc,uint8_t* p_data)
{
	uint16_t node_id = CAN_ID_GET_NODE(can_id);
	can_pct_pfn_node_t* p_next = m_can_pct_cb.p_head;
	while(p_next != NULL)
	{
		if(p_next->bus_type == type && p_next->can_id == node_id && p_next->tx_callback != NULL)
		{
			p_next->tx_callback(can_id,dlc,p_data);
		}
		p_next = p_next->next;
	}
}
void can_pct_rx_callback(can_bus_t type,uint16_t can_id,uint8_t dlc,uint8_t* p_data)
{
	uint16_t node_id = CAN_ID_GET_NODE(can_id);
	can_pct_pfn_node_t* p_next = m_can_pct_cb.p_head;
	while(p_next != NULL)
	{
		if(p_next->bus_type == type && p_next->can_id == node_id && p_next->rx_callback != NULL)
		{
			p_next->rx_callback(can_id,dlc,p_data);
			break;
		}
		p_next = p_next->next;
	}
}